<?php

namespace app\admin\controller\circle;

use app\common\controller\Backend;

/**
 * 文章管理
 *
 * @icon fa fa-circle-o
 */
class Comment extends Backend
{

    /**
     * Article模型对象
     * @var \app\admin\model\circle\Comment
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\circle\Comment;
        //获取用户列表
        $userModel = new \app\common\model\User();
        $userList = $userModel->order("id asc")->field('nickname,id')->select();
        $this->assign("userList",$userList);

        $userarr = $userModel->order("id asc")->column('nickname', 'id');
        $this->assignconfig("userarr",$userarr);
        $this->view->assign("userarr", $userarr);


        $fid=$this->request->param('fid');
        if(!$fid)$fid=0;
        $this->fid=$fid;
        $this->assignConfig('fid',$fid);
        $this->view->assign("fid", $fid);

    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        $level=$this->request->param('level');
        if(!$level)$level=0;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            $this->assignConfig('level',$level);
            return $this->view->fetch();
        }
        if ($this->request->isAjax()) {
            $level=$this->request->param('level');
            if(!$level)$level=0;
            //设置过滤方法
            $this->request->filter(['strip_tags', 'trim']);
            if (false === $this->request->isAjax()) {
                $this->assignConfig('level',$level);
                return $this->view->fetch();
            }
            //如果发送的来源是 Selectpage，则转发到 Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where('fid',$this->fid)
                ->order($sort, $order)
                ->paginate($limit);
            if($list->items()){
                foreach ($list->items() as $key=>$item){
                    $str="";
                    if($item['type']==1){
                        $str.="评论圈子";
                        $m = new \app\admin\model\circle\CircleList();
                        $info=$m->where(['id'=>$item['tid']])->field('content,id,pics')->find();
                        if($info)$str.="（".$info->id."）".$info->content;
                    }else{
                        $str.="评论评论";
                        $info=$this->model->where(['id'=>$item['tid']])->field('info,id')->find();
                        if($info)$str.="（".$info->id."）".$info->info;
                    }
                    $list->items()[$key]['tidname'] =$str;
                    $list->items()[$key]['childnum'] =$this->model->where(['fid'=>$item['id']])->count();
                }
            }
            $result = ['total' => $list->total(), 'rows' => $list->items()];
            return json($result);
        }
        return $this->view->fetch();
    }

}
